Repository navigation
fix(core): strip file marks inside templates and refuse markup a browser reads differently - #8845
Conversation
…ser reads differently
Edit accuracy: accurate 273 (base branch 271), smooth 68 of thoseThe gate passes. Newly passing (2)
Not banked yet, reported and not gated (bank a full-grid run's baseline.json) (2)
Quarantined, measured but not gated (0) |
vanceingalls
left a comment
There was a problem hiding this comment.
Request changes at 6b4f5a5: one blocker, a required check that this PR turns red.
Blocker: packages/cli/src/media-use/lib/svg-sanitize.mjs is stale. Test (support) fails, so the required Test job fails. The failing case is media-use svg-sanitize.mjs matches a fresh regeneration (scripts/check-media-use-svg-sanitize-generated.test.mjs), with the message svg-sanitize.mjs is stale — run: node scripts/generate-media-use-svg-sanitize.mjs. That file bundles sanitizeSvg.ts together with @hyperframes/parsers/linkedom, which this PR edits. The committed bundle does not contain the new --!> check. #8760 regenerated the bundle in its own commit. This is the PR's own red, not infra: the other Test run on main's head 5dd0ee9e failed in a different way, and its log has no is stale line. Fix: run node scripts/generate-media-use-svg-sanitize.mjs and commit the result. It matters beyond CI, because media-use's zero-install lane would otherwise keep the old, narrower refusal.
The rest checks out, from reading the diff (I ran nothing locally):
dropAuthoredCompositionFilesnow walks in document order. It reaches linkedom's real template children, nested templates and mixed-case attribute names. It readscontentonly for a template with no children, which is how a browser's template looks.- The
noscript,iframe,noembedandnoframescheck uses/</name[\t\n\f\r />]/ioninnerHTML. That covers upper case,</NOSCRIPT >and</noscript/, and it ignores</noscripts>. The--!>check looks only at comment nodes, so--!>inside a<script>string is not refused. Plainnoscriptfallbacks and comments still parse, and a test pins that. - The runtime fallback no longer re-parses a string. It imports the already-stripped nodes into a fragment and strips extracted assets before attaching, as the template and root branches do.
- Every case in the "fails without the fix" list is present, and each is a real regression test. The one that needs scripting on,
compositionLoader.noscript.test.ts, runs jsdom withrunScriptsset, as the preview does.
Notes for Miguel, not blocking. These are posture questions, not mine to answer:
hyperframes linton refused files. Lint rules callparseHTMLunguarded (rules/media.ts,rules/composition.ts,project.ts). A file that the parser now refuses therefore makes the wholelintcommand fail withruntime_error, instead of reporting a finding on that file. That is what #8760 did for the svg cases. This PR widens it to a--!>comment, which is valid HTML, and to end tags insidenoscriptandiframecomments. I read this from the code and did not run it. Should lint report a finding for these files? That could be a follow-up.- False positive. A
<noscript>inside<svg>is read as markup by a browser, but the check still refuses an end tag inside it. That input is very rare and fails safe.
|
Blocker fixed at I ran these on the regenerated tree:
Both notes on lint posture and the |
vanceingalls
left a comment
There was a problem hiding this comment.
Approving at 0799adb. The one blocker is fixed.
- The delta from 6b4f5a5 is one file,
packages/cli/src/media-use/lib/svg-sanitize.mjs(+6/-6), regenerated.parsers/src/linkedom.tsis byte-identical between 6b4f5a5 and 0799adb, so the old bundle was simply stale against its own source. - The new bundle now carries the
--!>constant and the comment-walk check, and its foreign-text logic (title/style/script/textarea/xmp inside svg/math, the noscript/iframe/noembed/noframes end-tag regex, the document-order walk, and theDOMParsertext/htmlrefusal) matcheslinkedom.tsline for line. The sanitizer tail is identical to the previous bundle apart from identifier renames, and no new code paths were added. - Required checks are green on this head, including
Test,Test (support),regression,Studio: edit accuracy gate,Tests on windows-latestandBuild.Heavy films: Safari vs Chromewas still running and is not a required check.
Two earlier non-blocking notes stand: lint rules call parseHTML unguarded, so a refused file turns into a runtime_error for the whole hyperframes lint run (posture call, follow-up is fine, same as #8760), and a rare noscript-inside-svg false positive that fails safe.
What
A
data-composition-filethat a composition file writes itself can no longer reach Studio's preview in two places that #8812 missed:<template>, when the server builds a page;</noscript>inside a comment in a<noscript>, and a comment that holds--!>.Why
Studio writes an element's edits to the file its nearest
data-composition-filenames. #8812 strips the marks a file authored before the build stamps its own. That covered elements the build parser can see, but not these two cases:querySelectorAllskips them, andtemplate.contentis a copy linkedom makes on first read. The strip ran on that copy, so the serialized page kept the marks, and the runtime later mounted them from the template.<noscript>,<iframe>,<noembed>or<noframes>as markup. A browser with scripting on reads it as text up to the first matching end tag, even one inside a comment or an attribute value. A browser also ends a comment at--!>, which htmlparser2 reads past. In both cases a mark hidden in a comment is never an element at build time, so the strip can't see it, yet the browser turns it into a real element.The runtime had the same gap of its own. When a fetched composition has no template or root, the runtime mounted its parsed body by setting
innerHTMLfrom a string. The page then re-read that string with scripting on.How
dropAuthoredCompositionFiles(packages/core/src/compositionSource.ts) walks the tree in document order, so it reaches a linkedom template's real children. It readscontentonly on a template with no children, which is how a browser's template looks. On linkedom the strip therefore no longer creates acontentcopy that later edits to the template would miss.parseHTMLandDOMParserinpackages/parsers/src/linkedom.tsalready refuse HTML that a browser would read as a different tree (raw-text elements inside svg and math). They now also refuse:noscript,iframe,noembedornoframesinside that element's own comments or attribute values;--!>.Every server path parses through them: the bundler, inlined sub-compositions, the render compile, studio-server's single-composition page and the saved page served when the build fails. All of them now refuse such a file the way they already refuse the svg cases.
The runtime's fallback mount imports the parsed nodes the strip already cleaned, into a detached fragment that is stripped of extracted assets before it is attached. That is what its template and root branches already do. Nothing in a fetched body is read a second time.
I checked which constructs reveal a mark, using linkedom to parse, strip and write, and parse5 with scripting on to read the result back:
noscript(including in<head>, inside templates and in any case),iframe,noembed,noframes--!><!-->,<!--->, CDATA, processing instructions,xmp,style,script,title,textarea,select,plaintext, and/, tab or form feed between attributesThe refusals cover every case that revealed a mark. Plain
<noscript>fallbacks,<iframe>s and comments still parse and bundle as before.No visible change
Previews and renders of ordinary compositions are unchanged. A file that hides an end tag in one of those elements, or a
--!>in a comment, now gets the same "can't be read the way a browser reads them" error as the svg cases.Studio already refused to paste a copied element that hides
</noscript>in an attribute. The parser now refuses it first, so the toast shows the parser's message, as it already does for the svg case.useClipboard.sealed.test.tsexpects that message for this one case. Nothing is written either way.Test plan
fails without the fix:
packages/core/src/compositionSource.test.ts:drops a mark inside a template at any depthandleaves a template's content to be made from its children when it is first read.packages/core/src/compiler/htmlBundler.test.ts:drops a mark authored inside a template, in the entry page or an inlined sub-composition.packages/core/src/compiler/htmlBundler.foreignText.test.ts: the fourrefuses ... in the root compositionandrefuses ... in a sub-compositioncases.packages/core/src/runtime/compositionLoader.noscript.test.ts:mounts a fetched body without a mark hidden in a noscript comment. This test runs jsdom with scripting on, as the preview does.packages/parsers/src/linkedomForeignText.test.ts: the nine newrefuses %s, which a browser reads differentlycases.packages/studio-server/src/helpers/subComposition.test.ts:drops a mark written inside a template, at any depthandrefuses a mark hidden where a scripting browser reads past a comment.packages/studio-server/src/routes/preview.unbuiltFileMarks.test.ts: the four existing cases, whose fixture now also has a marked element inside a template.Every one of these fails on main and passes with the fix, three runs in a row.
Two further assertions in
compositionLoader.test.tscover the browser branch: a template inside a fetched file is stripped through itscontent. They pass on main too. Removing thecontentbranch fails them.